package com.atguigu.gulimall.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 1. 导入依赖
 * 2. 编写配置, 给容器中注入一个RestHighLevelClient
 * 3. 参照官方的API进行操作就行了
 *
 * ES 做数据迁移
 */
@Configuration
public class GulimallElasticSearchConfig {


    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
//        builder.addHeader("Authorization", "Bearer " + TOKEN);
//        builder.setHttpAsyncResponseConsumerFactory(
//                new HttpAsyncResponseConsumerFactory
//                        .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
        COMMON_OPTIONS = builder.build();
    }


    @Bean
    public RestHighLevelClient esRestClient() {

        /**
         * Creates {@code HttpHost} instance with the given scheme, hostname and port.
         *
         * @param hostname  the hostname (IP or DNS name)
         * @param port      the port number.
         *                  {@code -1} indicates the scheme default port.
         * @param scheme    the name of the scheme.
         *                  {@code null} indicates the
         *                  {@link #DEFAULT_SCHEME_NAME default scheme}
         */

        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("172.18.0.3", 9200, "http")));  // 这里端口号应该是9200

        return client;
    }
}
